package com.jeeplus.cms.util;

import com.jeeplus.common.utils.CacheUtils;
import com.jeeplus.common.utils.SpringContextHolder;
import com.jeeplus.modules.cms.column.entity.CmsColumn;
import com.jeeplus.modules.cms.column.service.CmsColumnService;
import com.jeeplus.modules.cms.content.entity.CmsContents;
import com.jeeplus.modules.cms.content.service.CmsContentsService;
import com.jeeplus.modules.cms.contenttype.entity.CmsContentType;
import com.jeeplus.modules.cms.contenttype.service.CmsContentTypeService;
import com.jeeplus.modules.cms.dm.entity.CmsDynamic;
import com.jeeplus.modules.cms.dm.service.CmsDynamicService;
import com.jeeplus.modules.cms.dyn.entity.CmsContentsDyn;
import com.jeeplus.modules.cms.dyn.service.CmsContentsDynService;
import com.jeeplus.modules.cms.link.entity.CmsLink;
import com.jeeplus.modules.cms.link.service.CmsLinkService;
import com.jeeplus.modules.cms.model.entity.CmsModel;
import com.jeeplus.modules.cms.model.service.CmsModelService;
import com.jeeplus.modules.cms.site.entity.CmsSite;
import com.jeeplus.modules.cms.site.service.CmsSiteService;
import com.jeeplus.modules.cms.theme.entity.CmsThemes;
import com.jeeplus.modules.cms.theme.service.CmsThemesService;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletContext;
import java.util.Collections;
import java.util.List;

/**
 * 缓存工具类
 *
 * @author Sawyer
 * @date 2020-12-09
 */
public class CmsCacheUtils {

    private static CmsSiteService siteService = SpringContextHolder.getBean(CmsSiteService.class);
    private static CmsThemesService themesService = SpringContextHolder.getBean(CmsThemesService.class);
    private static CmsLinkService linkService = SpringContextHolder.getBean(CmsLinkService.class);
    private static CmsColumnService columnService = SpringContextHolder.getBean(CmsColumnService.class);
    private static CmsContentTypeService contentTypeService = SpringContextHolder.getBean(CmsContentTypeService.class);
    private static CmsDynamicService dynamicService = SpringContextHolder.getBean(CmsDynamicService.class);
    private static CmsModelService modelService = SpringContextHolder.getBean(CmsModelService.class);
    private static CmsContentsService contentsService = SpringContextHolder.getBean(CmsContentsService.class);
    private static CmsContentsDynService contentsDynService = SpringContextHolder.getBean(CmsContentsDynService.class);
    private static ServletContext context = SpringContextHolder.getBean(ServletContext.class);

    private static final String CMS_CACHE = "cmsCache";

    /**
     * 验证码缓存
     */
    private static final String CAPTCHA_CACHE = "captchaCache";

    /**
     * 站点缓存key
     */
    public static final String SITE_LIST_CACHE = "siteList";

    /**
     * 主题缓存key
     */
    public static final String THEME_LIST_CACHE = "themeList";

    /**
     * 栏目缓存key
     */
    public static final String COLUMN_LIST_CACHE = "columnList";

    /**
     * 模型缓存key
     */
    public static final String MODEL_LIST_CACHE = "modelList";

    /**
     * 内容分类缓存key
     */
    public static final String CONTENT_TYPE_LIST_CACHE = "contentTypeList";

    /**
     * 内容缓存key
     */
    public static final String CONTENT_LIST_CACHE = "contentList";

    /**
     * 动态内容缓存key
     */
    public static final String CONTENT_DYN_LIST_CACHE = "contentDynList";

    /**
     * 动态表单缓存key
     */
    public static final String DYNAMIC_LIST_CACHE = "dynamicList";

    /**
     * 链接缓存key
     */
    public static final String LINK_LIST_CACHE = "linkList";

    /**
     * 缓存key连接符
     */
    private static final String CACHE_KEY_DELIMITER = "_";

    public static Object getCache(String key) {
        return CacheUtils.get(CMS_CACHE, key);
    }

    public static void putCache(String key, Object value) {
        CacheUtils.put(CMS_CACHE, key, value);
    }

    public static Object getCaptcha(String key) {
        return CacheUtils.get(CAPTCHA_CACHE, key);
    }

    public static void putCaptcha(String key, Object value) {
        CacheUtils.put(CAPTCHA_CACHE, key, value);
    }

    public static void removeCache(String key) {
        CacheUtils.remove(CMS_CACHE, key);
    }

    public static Object getCacheBySiteId(String type, String siteId) {
        return CacheUtils.get(CMS_CACHE, type + CACHE_KEY_DELIMITER + StringUtils.trimToEmpty(siteId));
    }

    public static void putCacheBySiteId(String type, String siteId, Object value) {
        CacheUtils.put(CMS_CACHE, type + CACHE_KEY_DELIMITER + StringUtils.trimToEmpty(siteId), value);
    }

    public static void removeCacheBySiteId(String type, String siteId) {
        CacheUtils.remove(CMS_CACHE, type + CACHE_KEY_DELIMITER + StringUtils.trimToEmpty(siteId));
    }

    /**
     * 获取网站信息
     *
     * @return list
     */
    public static List<CmsSite> getSiteListByCache() {
        return getSiteListByCache(true);
    }

    /**
     * @param loadIfNotFound 数据不存在是否从数据库中加载
     */
    public static List<CmsSite> getSiteListByCache(boolean loadIfNotFound) {
        List<CmsSite> list = (List<CmsSite>) getCache(SITE_LIST_CACHE);
        boolean isLoad = (list == null || list.isEmpty()) && loadIfNotFound;
        if (isLoad) {
            list = siteService.findAllList(new CmsSite());
            putCache(SITE_LIST_CACHE, list);
        }
        return null == list ? Collections.emptyList() : list;
    }


    /**
     * 获取内容分类列表
     *
     * @return list
     */
    public static List<CmsContentType> getContentTypeListByCache(String siteId) {
        return getContentTypeListByCache(siteId, true);
    }

    public static List<CmsContentType> getContentTypeListByCache(String siteId, boolean loadIfNotFound) {
        List<CmsContentType> list = (List<CmsContentType>) getCacheBySiteId(CONTENT_TYPE_LIST_CACHE, siteId);
        boolean isLoad = (list == null || list.isEmpty()) && loadIfNotFound;
        if (isLoad) {
            CmsContentType condition = new CmsContentType();
            condition.setSiteId(siteId);
            list = contentTypeService.findAllList(condition);
            putCacheBySiteId(CONTENT_TYPE_LIST_CACHE, siteId, list);
        }
        return null == list ? Collections.emptyList() : list;
    }

    /**
     * 获取模型列表
     *
     * @return list
     */
    public static List<CmsModel> getModelListByCache(String siteId) {
        return getModelListByCache(siteId, true);
    }

    public static List<CmsModel> getModelListByCache(String siteId, boolean loadIfNotFound) {
        List<CmsModel> list = (List<CmsModel>) getCacheBySiteId(MODEL_LIST_CACHE, siteId);
        boolean isLoad = (list == null || list.isEmpty()) && loadIfNotFound;
        if (isLoad) {
            CmsModel condition = new CmsModel();
            condition.setSiteId(siteId);
            list = modelService.findAllList(condition);
            putCacheBySiteId(MODEL_LIST_CACHE, siteId, list);
        }
        return null == list ? Collections.emptyList() : list;
    }

    /**
     * 获取内容列表
     *
     * @return list
     */
    public static List<CmsContents> getContentListByCache(String siteId) {
        return getContentListByCache(siteId, true);
    }

    public static List<CmsContents> getContentListByCache(String siteId, boolean loadIfNotFound) {
        List<CmsContents> list = (List<CmsContents>) getCacheBySiteId(CONTENT_LIST_CACHE, siteId);
        boolean isLoad = (list == null || list.isEmpty()) && loadIfNotFound;
        if (isLoad) {
            CmsContents condition = new CmsContents();
            condition.setSiteId(siteId);
            list = contentsService.findAllList(condition);

            putCacheBySiteId(CONTENT_LIST_CACHE, siteId, list);
        }
        return null == list ? Collections.emptyList() : list;
    }

    /**
     * 获取动态内容列表
     *
     * @return list
     */
    public static List<CmsContentsDyn> getContentDynListByCache(String siteId) {
        return getContentDynListByCache(siteId, true);
    }

    public static List<CmsContentsDyn> getContentDynListByCache(String siteId, boolean loadIfNotFound) {
        List<CmsContentsDyn> list = (List<CmsContentsDyn>) getCacheBySiteId(CONTENT_DYN_LIST_CACHE, siteId);
        boolean isLoad = (list == null || list.isEmpty()) && loadIfNotFound;
        if (isLoad) {
            CmsContentsDyn condition = new CmsContentsDyn();
            condition.setSiteId(siteId);
            list = contentsDynService.findAllList(condition);

            putCacheBySiteId(CONTENT_DYN_LIST_CACHE, siteId, list);
        }
        return null == list ? Collections.emptyList() : list;
    }

    /**
     * 获取栏目列表
     *
     * @return list
     */
    public static List<CmsColumn> getColumnListByCache(String siteId) {
        return getColumnListByCache(siteId, true);
    }

    public static List<CmsColumn> getColumnListByCache(String siteId, boolean loadIfNotFound) {
        List<CmsColumn> list = (List<CmsColumn>) getCacheBySiteId(COLUMN_LIST_CACHE, siteId);
        boolean isLoad = (list == null || list.isEmpty()) && loadIfNotFound;
        if (isLoad) {
            // 从数据库中获取有效的栏目放入缓存中
            CmsColumn condition = new CmsColumn();
            condition.setSiteId(siteId);
            list = columnService.findAllList(condition);

            putCacheBySiteId(COLUMN_LIST_CACHE, siteId, list);
        }
        return null == list ? Collections.emptyList() : list;
    }

    /**
     * 获取主题列表
     *
     * @return list
     */
    public static List<CmsThemes> getThemeListByCache() {
        return getThemeListByCache(true);
    }

    public static List<CmsThemes> getThemeListByCache(boolean loadIfNotFound) {
        List<CmsThemes> list = (List<CmsThemes>) getCache(THEME_LIST_CACHE);
        boolean isLoad = (list == null || list.isEmpty()) && loadIfNotFound;
        if (isLoad) {
            // 从数据库中获取有效的条目放入缓存中
            CmsThemes condition = new CmsThemes();
            list = themesService.findAllList(condition);

            putCache(THEME_LIST_CACHE, list);
        }
        return null == list ? Collections.emptyList() : list;
    }

    /**
     * 获取动态表单列表
     *
     * @return list
     */
    public static List<CmsDynamic> getDynamicListByCache(String siteId) {
        return getDynamicListByCache(siteId, true);
    }

    public static List<CmsDynamic> getDynamicListByCache(String siteId, boolean loadIfNotFound) {
        List<CmsDynamic> list = (List<CmsDynamic>) getCacheBySiteId(DYNAMIC_LIST_CACHE, siteId);
        boolean isLoad = (list == null || list.isEmpty()) && loadIfNotFound;
        if (isLoad) {
            // 从数据库中获取有效的条目放入缓存中
            CmsDynamic condition = new CmsDynamic();
            condition.setSiteId(siteId);
            list = dynamicService.findAllList(condition);

            putCacheBySiteId(DYNAMIC_LIST_CACHE, siteId, list);
        }
        return null == list ? Collections.emptyList() : list;
    }

    /**
     * 获取友情链接列表
     *
     * @return list
     */
    public static List<CmsLink> getLinkListByCache(String siteId) {
        return getLinkListByCache(siteId, true);
    }

    public static List<CmsLink> getLinkListByCache(String siteId, boolean loadIfNotFound) {
        List<CmsLink> list = (List<CmsLink>) getCacheBySiteId(LINK_LIST_CACHE, siteId);
        boolean isLoad = (list == null || list.isEmpty()) && loadIfNotFound;
        if (isLoad) {
            // 从数据库中获取有效的条目放入缓存中
            CmsLink condition = new CmsLink();
            condition.setSiteId(siteId);
            list = linkService.findAllList(condition);

            putCacheBySiteId(LINK_LIST_CACHE, siteId, list);
        }
        return null == list ? Collections.emptyList() : list;
    }
}
